<?php
class RM_Module_Reviews extends RM_Module implements RM_Unit_Details_Tray_Interface, RM_Unit_List_Tray_Interface, RM_Search_Advanced_Interface {

    /**
     * This string is for rm_reservation_summary table
     */
    const SUMMARY_TYPE = 'reviews';

    public function  __construct()
    {
        $this->name = 'Reviews';
    }

    public function deleteLanguage($locale)
    {
        parent::deleteLanguage($locale);
        $model = new RM_Reviews;
        $model->deleteLanguage($locale);
    }

    public function addLanguage($locale)
    {
        parent::addLanguage($locale);
        $model = new RM_Reviews;
        $model->addLanguage($locale);
    }

    /**
     * This method will return node object for main admin menu tree.
     * Every child classes could overload this method to return any of the node object.
     * If there is no need to present a module in the main admin tree overloaded method should return NULL
     *
     * @return stdClass | null
     */
    public function getNode()
    {
        $std = new stdClass;

        $std->id = $this->name.'_ListJson_NoAjax';
        $std->text = $this->getName();

        $std->iconCls = 'RM_modules_leaf_icon';
        $std->leaf = 'true';
        return $std;
    }


	/**
	* get an array of the id's that match the search criteria
	*/
    public function getAdvancedSearchUnitIDs(RM_Unit_Search_Criteria $criteria){

        if (!$criteria->reviews){
            return false;
        }

        // just check if we acutally have any reviews selected...
//        $reviewsSet = false;
//        foreach($criteria->reviews as $key=>$value){
//            if ($value === "1"){
//                $reviewsSet = true;
//            }
//        }
//        if (!$reviewsSet){
//            return false;
//        }
//
//        $currentIso = RM_Environment::getInstance()->getLocale();
//
//        $model = new RM_Reviews();
//
//        $amenityIDs = array();
//
//        // get all the unit id's this is for when the reviews are set to 0
//        $unitsModel = new RM_Units();
//        $units = $unitsModel->getAll(new RM_Unit_Search_Criteria())->toArray();
//        foreach($units as $unit){
//            $allUnitArray[] = $unit['id'];
//        }
//
//        $amenitysIDArray = array();
//
//        // get the selected reviews and add to an array
//        foreach($criteria->reviews as $key=>$value){
//            if ($value === "1"){
//                $data = $model->getReviewsDetails($key)->toArray();
//                foreach($data as $item){
//                    if (isset($item['unit_id'])){
//                        $amenitysIDArray[] = $item['unit_id'];
//                    }
//                }
//
//            }
//        }
//
//        return $amenitysIDArray;
    }
    
    /**
    * Install routine
    */
    public function install() {
        
    }
    
    /**
     * uninstall routine
     */
    public function uninstall() {
        
    }

    /**
     * This gets the HTML for the module try Icon
     *
     * @param RM_Unit_Row $unit
     * @return HTML
     */
    public function getHTML(RM_Unit_Row $unit){

        // get from config if we want to show the icon
        // but for now as an example we will just set this
        $config['show_details'] = "1";

        if ($config['show_details'] === "1"){
            $icon = RM_Environment::getConnector()->getRootURL().'RM/userdata/modules/Reviews/images/tray_icon.png';
            return "<a href='javascript:void(0)' onclick=\"RM_doShadowBox('".RM_Environment::getInstance()->getRouter()->_('Reviews', 'showreviews', array("unit_id"=>$unit->id, "page" => "reviews.phtml"))."', '', 400, 200, 'iframe' )\" ><img src='".$icon."' border='0'></a>";
        }
        return false;
    }

    public function getListHTML(RM_Unit_Row $unit){

        $config['show_list'] = "1";

        if ($config['show_list'] === "1"){
            $icon = RM_Environment::getConnector()->getRootURL().'RM/userdata/modules/Reviews/images/tray_icon.png';
            return "<a href='javascript:void(0)' onclick=\"RM_doShadowBox('".RM_Environment::getInstance()->getRouter()->_('Reviews', 'showreviews', array("unit_id"=>$unit->id, "page" => "reviews.phtml"))."', '', 400, 200, 'iframe' )\" ><img width='30px' height='30px' src='".$icon."' border='0'></a>";
        }
        return false;
    }
}
